<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2017/12/19
 * Time: 下午11:12
 */

namespace backend\controllers;
use Yii;
use common\models\RentingBrandModel;
use common\logic\RentingBrandLogic;
use common\logic\RentingLogic;

use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;
use yii\helpers\ArrayHelper;

class RentingBrandController extends BaseController
{
	public function actionIndex()
    {
        
		$query = RentingBrandModel::find();
        $query->orderBy(['ph_renting_brand.id'=>SORT_DESC]);
        
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
				
		$dataProvider->sort = false;		
        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }
	
    public function actionCreate()
    {
        $model = new RentingBrandModel();
//      $model->id = 0;
//      $model->bname = '';
//		$model->brandloge='';
//		$model->title='';
		
        $post = Yii::$app->request->post();
		
//		$model->load($post);
//		var_dump($model);
//		exit();
		if(isset($post['RentingBrandModel']) && isset($post['RentingBrandModel']['bname'])){
			$res = RentingBrandLogic::instance()->getByName($post['RentingBrandModel']['bname']);
			if(count($res)>0){
				$model->load($post);
				
				return $this->render('create', [
	                'model' => $model,
	                'error'=>'品牌名字重复'
	            ]);
			}
		}
		
		
        if ($model->load($post) && RentingBrandLogic::instance()->RentingBrandSave($model)) {
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
                'error'	=> 'ok'
            ]);
        }
    }
	
    /**
     * Deletes an existing HouseModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionDelete($id)
    {
    	
		$nums = RentingLogic::instance()->getBrandNum($id);
		if($nums > 0){
			exit(json_encode(['error'=>'该品牌已经在租房列表中被使用']));
		}
		
        $this->findModel($id)->delete();
        RentingBrandModel::deleteAll(['id'=>$id]);
		
        return $this->redirect(['index']);
    }
    
    /**
     * Finds the HouseModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     *
     * @param integer $id
     *
     * @return HouseModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = RentingBrandModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function actionUpdate($id)
    {
        
		 $model = $this->findModel($id);
		 $post = Yii::$app->request->post();
		
//		var_dump($post);
//		exit();
		
		if(isset($post['RentingBrandModel']) && isset($post['RentingBrandModel']['bname'])){
			$id = $post['RentingBrandModel']['id'];
			$res = RentingBrandLogic::instance()->getByNameId($post['RentingBrandModel']['bname'],$id);
			if(count($res)>0){
				unset($post['RentingBrandModel']['brandloge']);
				$model->load($post);
				return $this->render('create', [
	                'model' => $model,
	                'error'=>'品牌名字重复'
	            ]);
			}
		}
		
		unset($post['RentingBrandModel']['brandloge']);
		
        if ($model->load($post) && RentingBrandLogic::instance()->RentingBrandSave($model)) {
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
                'error'	=> 'ok'
            ]);
        }
		
    }
}